package com.itheima.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.MoneyInfoParam;
import com.itheima.dto.OrderDelDto;
import com.itheima.dto.OrderListDto;
import com.itheima.dto.ReceiverInfoParam;
import com.itheima.excption.MyException;


import com.itheima.holder.AdminLocal;
import com.itheima.pojo.OmsOrder;
import com.itheima.pojo.OmsOrderItem;
import com.itheima.pojo.OmsOrderOperateHistory;
import com.itheima.vo.OrderVo;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OmsOrderService {

    @DubboReference
    private OmsOrderApi omsOrderApi;
    @Autowired
    private OmsOrderOperateHistoryService omsOrderOperateHistoryService;
    @DubboReference
    private OmsOrderItemApi omsOrderItemApi;

    /**
     * 逻辑批量删除
     * 将状态改为5 无效订单
     *
     * @param ids
     */
    public void deleteLogic(Integer[] ids) {
        //只能删除已关闭的订单
        for (Integer id : ids) {
            OmsOrder omsOrder = omsOrderApi.findById(id);
            if (omsOrder.getStatus() !=4||omsOrder.getStatus()!=5) {
                throw new MyException("删除失败!只能删除已关闭或者无效订单");
            }
        }


        omsOrderApi.deleteLogic5(ids);
    }


    /**
     * @param orderListDto
     * @return
     */
    public PageResultVo<OmsOrder> listPage(OrderListDto orderListDto) {
        IPage<OmsOrder> page = omsOrderApi.listPage(orderListDto);
        PageResultVo<OmsOrder> pageResultVo = new PageResultVo<>();
        pageResultVo.setPageNum(orderListDto.getPageNum());
        pageResultVo.setPageSize(orderListDto.getPageSize());
        Long total = page.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = page.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        pageResultVo.setList(page.getRecords());
        return pageResultVo;
    }


    /**
     * 批量关闭订单
     *
     * @param ids
     * @param note
     */
    public void closeByIds(Integer[] ids, String note) {
        //先进行查询
        for (Integer id : ids) {
            OmsOrder omsOrder = omsOrderApi.findById(id);
            if (omsOrder.getStatus() == 4) {
                throw new MyException("关闭失败!选项中包含已关闭的订单");
            }
            if (omsOrder.getStatus() != 1 && omsOrder.getStatus() != 4) {
                throw new MyException("关闭失败!只能关闭待付款的订单");
            }
        }
       String username = AdminLocal.getAdmin().getUsername();
        omsOrderApi.close(ids, note,username);


    }

    /**
     * 批量发货
     *
     * @param deliveryParamList
     */
    public void updateDel(List<OrderDelDto> deliveryParamList) {
        //先根据 id 查询出 订单的状态 只能发货 为发货的订单
        for (OrderDelDto orderDelDto : deliveryParamList) {
            OmsOrder omsOrder = omsOrderApi.findById(orderDelDto.getOrderId());
            if (omsOrder.getStatus() != 1) {
                throw new MyException("发货失败! 选项中包含非待发货状态的订单");
            }
        }
        omsOrderApi.updateDel(deliveryParamList);

    }

    public void moneyInfo(MoneyInfoParam moneyInfoParam) {
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        omsOrder.setFreightAmount(moneyInfoParam.getFreightAmount());
        omsOrder.setId(moneyInfoParam.getOrderId().longValue());
        omsOrder.setStatus(moneyInfoParam.getStatus());

        omsOrderApi.updateById(omsOrder);
    }


    /**
     * 修改当前订单的备注
     * 并将操作记录保存到操作记录表中
     *
     * @param id
     * @param note
     * @param status
     */
    public void updateAndSave(Integer id, String note, Integer status) {
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(id.longValue());
        omsOrder.setNote(note);
        //根据id修改备注
        omsOrderApi.updateById(omsOrder);

        //将操作记录保存到操作记录表中
        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
        omsOrderOperateHistory.setOrderId(id.longValue());
        String username = AdminLocal.getAdmin().getUsername();
        omsOrderOperateHistory.setOperateMan("后台管理员"+username);
        omsOrderOperateHistory.setNote(note);
        omsOrderOperateHistory.setOrderStatus(status);
        omsOrderOperateHistory.setCreateTime(LocalDateTime.now());
        omsOrderOperateHistoryService.save(omsOrderOperateHistory);
    }


    /**
     * 修改个人信息
     *
     * @param receiverInfoParam
     */
    public void updateReceiverInfo(ReceiverInfoParam receiverInfoParam) {
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(receiverInfoParam.getOrderId().longValue());
        omsOrder.setReceiverCity(receiverInfoParam.getReceiverCity());
        omsOrder.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        omsOrder.setReceiverName(receiverInfoParam.getReceiverName());
        omsOrder.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        omsOrder.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        omsOrder.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        omsOrder.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        omsOrderApi.updateById(omsOrder);
        //将操作记录保存到操作记录表中
        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
        omsOrderOperateHistory.setOrderId(receiverInfoParam.getOrderId().longValue());
        String username = AdminLocal.getAdmin().getUsername();
        omsOrderOperateHistory.setOperateMan("后台管理员"+username);
        omsOrderOperateHistory.setNote("修改收货人信息");
        omsOrderOperateHistory.setOrderStatus(receiverInfoParam.getStatus());
        omsOrderOperateHistory.setCreateTime(LocalDateTime.now());
        omsOrderOperateHistoryService.save(omsOrderOperateHistory);

    }

    /**
     * 根据id查询订单详情
     *
     * @param id
     * @return
     */
    public OrderVo getById(Integer id) {
        //先查询出订单的基本信息
        OmsOrder omsOrder = omsOrderApi.findById(id);

        //再根据订单中的商品的查询出对应的
        List<OmsOrderItem> omsOrderItems = omsOrderItemApi.findByOrderId(id);

        //再根据订单的id查询出历史记录
        List<OmsOrderOperateHistory> histories = omsOrderOperateHistoryService.findByOrderId(id);

        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(omsOrder, orderVo);

        orderVo.setOrderItemList(omsOrderItems);
        orderVo.setHistoryList(histories);
        return orderVo;
    }
}
